package cn.itsy.web.controller;

import cn.itsy.dao.Pagedao;
import cn.itsy.pojo.*;
import cn.itsy.service.dept.DeptService;
import cn.itsy.service.job.JobService;
import cn.itsy.service.position.PositionService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("dept")
public class DeptController {

    //    dept的service层引用
    @Resource(name = "deptServiceimpl")
    private DeptService deptService;

    @Resource(name = "positionServiceimpl")
    private PositionService positionService;

    @Resource(name = "jobServiceimpl")
    private JobService jobService;

    private List<JobAndEmpAndDept> resultSet;

    @Resource
    private Pagedao pagedao;

    /**
     * 添加部门的方法
     *
     * @param dept
     */
    @RequestMapping(value = "insertDept", method = RequestMethod.GET)
    @ResponseBody
    public void insertDept(Dept dept) {
        String insertSql = "insert into dept values(?,?,?,?,?,?,?,?,?,?)";
        boolean b = this.deptService.insertDept(insertSql, dept);
    }

    /**
     * 查询的方法
     */
    @RequestMapping("queryAll")
    @ResponseBody
    public List<Dept> queryDeptAll() {
        String sql = "select dept.*,wname from dept,position where dept.pid=position.id";
        List<Dept> depts = this.deptService.queryAll(sql);
        Page page = new Page();
//        this.pagedao.deptShowPage(sql,,page);
        return depts;
    }

    /**
     * 修改跳页面
     */
    @RequestMapping(value = "forwardUpdate")
    public String queryDeptById(Dept dept, Model model, String checkText) {
        String sql = "select dept.*,wname from dept,position where dept.pid=position.id and dept.id=?";
        Dept dept1 = this.deptService.queryDeptById(sql, dept.getId());
        String querySql = " select * from position ";
        List<Position> positions = this.positionService.queryPositionAll(querySql);
        model.addAttribute("dept", dept1);
        model.addAttribute("positions", positions);
        model.addAttribute("check", checkText);
        if (dept.getId() == null) {
            return "redirect:/myloginfo.jsp";
        }
        return "/MymodelAndView";
    }

    /**
     * 确认修改
     */
    @RequestMapping(value = "startUpdateDept", method = RequestMethod.POST)
    public String startUpdateDept(Dept dept, String ck) {
        String sql = "update dept set dname=?,dmanager=?,dtel=?,dfax=?,demail=?," +
                "duty=? where id=?";
        boolean b = this.deptService.updateDeptById(sql, dept, ck);
        if (b) {
            return "redirect:/showDeptData.jsp";
        }
        return "redirect:/404.jsp";
    }

    /**
     * 删除部门
     */
    @RequestMapping(value = "delDeptById", method = RequestMethod.POST)
    @ResponseBody
    public Integer deleteDeptById(@RequestParam Integer id) {
        String querySql = "select * from dept,emp where emp.depart_id=dept.id and emp.depart_id=?";
        List<EmpAndDept> empAndDepts = this.deptService.queryEmpAndDeptById(querySql, id);
        if (empAndDepts.isEmpty()) {
            String delSql = "delete from dept where dept.id=? ";
            this.deptService.deleteDeptById(delSql, id);
            String sql = "delete from job where job.dept_id=? ";
            this.jobService.deleteJobBuId(sql, id);
            return 1;
        }
        return 0;
    }

    /**
     * 准备移动部门
     */
    @RequestMapping(value = "moveDept", method = RequestMethod.GET)
    public String moveDept(Integer id, Model model) {
        String sql = "select dept.*,wname from dept,position where dept.pid=position.id and dept.id=?";
        Dept dept1 = this.deptService.queryDeptById(sql, id);
        String querySql = " select * from position ";
        List<Position> positions = this.positionService.queryPositionAll(querySql);
        model.addAttribute("dept", dept1);
        model.addAttribute("positions", positions);
        return "/stratModeDept";
    }

    /**
     * 开始移动部门
     */
    @RequestMapping(value = "stratMoveDept", method = RequestMethod.POST)
    public String stratMoveDept(Integer pid, Integer id) {
        String sql = "update dept set pid =? where id=?";
        boolean b = this.deptService.updateDeptById(sql, pid, id);
        if (b) {
            return "redirect:/showDeptData.jsp";
        }
        return "redirect:/404.jsp";
    }

    /**
     * 准备合并部门
     */
    @RequestMapping(value = "groupDept", method = RequestMethod.GET)
    @ResponseBody
    public void groupDept(String[] ids) {
        Integer[] id = new Integer[ids.length];
        int sub = 0;
        for (String i : ids) {
            if (i.charAt(0) == '[') {
                i = i.replaceAll("\\[", "");
            }
            if (i.charAt(i.length() - 1) == ']') {
                i = i.replaceAll("]", "");
            }
            if (!"".equals(i) && i != null) {
                i = i.substring(1, i.length() - 1);
                id[sub] = new Integer(i);
                sub++;
            }
        }
        String sql = "select * from dept where dept.id=? or dept.id=? ";
        resultSet = this.deptService.queryDeptsById(sql, id);
    }

    /**
     * 开始合并部门
     *
     * @param dept
     */
    @RequestMapping(value = "stratGroupDept")
    @ResponseBody
    public void stratGroupDept(Dept dept) {
        String sql = "insert into dept values(?,?,?,?,?,?,?,?,?,?)";
        boolean b = this.deptService.insertDept(sql, dept);
        String queryLastId = "select max(dept.id) as id from dept";
        Integer[] ids = new Integer[resultSet.size() + 1];
        int sub = 0;
        for (JobAndEmpAndDept jobAndEmpAndDept : resultSet) {
            ids[sub] = jobAndEmpAndDept.getId();
            sub++;
        }
        Dept dept1 = this.deptService.queryInsertNewId(queryLastId);
        ids[ids.length - 1] = dept1.getId();
        String updataDeptsql = "update emp set emp.depart_id=? where emp.depart_id=? or emp.depart_id=?";
        boolean b1 = this.deptService.updateEmpAndJobById(updataDeptsql, ids);
        String jobSql = "update job set job.dept_id=? where job.dept_id=? or job.dept_id=?";
        boolean b2 = this.deptService.updateJobById(jobSql, ids);
        String deleteDeptSql = "delete from dept where dept.id=? or dept.id=? ";
        this.deptService.deleteDeptByIds(deleteDeptSql, ids);
    }
}
